﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_QuanLyNamHoc_KyHoc : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                TangMa(lblMa);
                TangMa1(lblKy);
                LoadKy();
                Load();
            }
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
       
        NamHoc nh = new NamHoc();
        nh.MaNam = int.Parse(lblMa.Text);
        nh.TenNam = txtNam.Text;
        db.NamHocs.InsertOnSubmit(nh);
        db.SubmitChanges();
        Load();
        TangMa(lblMa);
        txtNam.Text = "";
    }
    void Load()
    {
        var c = from p in db.NamHocs select p;
        grvNamHoc.DataSource = c;
        grvNamHoc.DataBind();
    }
    void TangMa(Label ma)
    {
        var t = from p in db.NamHocs orderby p.MaNam descending select p.MaNam;
        string tmp = t.FirstOrDefault().ToString();
        if (tmp == "")
        {
            ma.Text = "1";
        }
        else
            ma.Text = (int.Parse(tmp.Trim()) + 1).ToString();
    }
    void TangMa1(Label ma)
    {
        var t = from p in db.KyHocs orderby p.MaKy descending select p.MaKy;
        string tmp = t.FirstOrDefault().ToString();
        if (tmp == "")
        {
            ma.Text = "1";
        }
        else
            ma.Text = (int.Parse(tmp.Trim()) + 1).ToString();
    }
        protected void btnSua_Click(object sender, EventArgs e)
        {
            
            GridViewRow row = grvNamHoc.SelectedRow;
            Label Ma = (Label)row.FindControl("lblMaNam");
            NamHoc nh = db.NamHocs.SingleOrDefault(p=>p.MaNam==int.Parse(Ma.Text));
            nh.TenNam = txtNam.Text;
            db.SubmitChanges();
            Load();
            txtNam.Text = "";

        }
        protected void btnTaoMoi_Click(object sender, EventArgs e)
        {
            txtNam.Text = "";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            KyHoc kh = new KyHoc();
            kh.MaKy = int.Parse(lblKy.Text);
            kh.TenKy = txtTenKy.Text;
            db.KyHocs.InsertOnSubmit(kh);
            db.SubmitChanges();
            LoadKy();
            TangMa1(lblKy);
            txtTenKy.Text = "";
        }
        void LoadKy()
        {
            var c = from p in db.KyHocs select p;
            grvBanHoc.DataSource = c;
            grvBanHoc.DataBind();
        }
        protected void grvBanHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = grvBanHoc.SelectedRow;
            Label MaKy = (Label)row.FindControl("lblMaKy");
            KyHoc kh = db.KyHocs.SingleOrDefault(p=>p.MaKy==int.Parse(MaKy.Text));
            txtTenKy.Text = kh.TenKy;
        }
        protected void btnUpDate_Click(object sender, EventArgs e)
        {
            GridViewRow row = grvBanHoc.SelectedRow;
            Label MaKy = (Label)row.FindControl("lblMaKy");
            KyHoc kh = db.KyHocs.SingleOrDefault(p => p.MaKy == int.Parse(MaKy.Text));
            kh.TenKy = txtTenKy.Text;
            db.SubmitChanges();
            txtTenKy.Text = "";
            LoadKy();

        }
        protected void btnNew_Click(object sender, EventArgs e)
        {
            txtTenKy.Text = "";

        }
        protected void grvNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = grvNamHoc.SelectedRow;
            Label Ma = (Label)row.FindControl("lblMaNam");
            NamHoc nh = db.NamHocs.SingleOrDefault(p => p.MaNam == int.Parse(Ma.Text));
            txtNam.Text = nh.TenNam;

        }
}
